package com.lian.anlimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lian.anlimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000117648599";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCOWk+NHPGpZ2u7QwNvACw+5kC/c+wt7lF7CQ4JwNjnsmkWPlbxhNFd5WecDZE6V6pqs2bkVoNxvs8K54U6u11Kc5KqueyJwvG6xT8xoqZ8ABQOguFMjSLNXUoIUX9AVgo8g1LXQkoNnaJ9vw7wyxGiKV4sU9qKT+gZFiV2xdIgK5cv83fnck05KYGS0aKfwjHChlIIDXKfHuBmY+x17vUHKFssa1h2q/kYF4aWSWjfJRb3UaJ1Co0Scl2Fg8PqgSaHnPHvuU/2DlBj8w1+u63efN3v+k9z+1G3p7jP6s0QTyr+az2rLTDAPlGCJFy6hxbhAh7/R0nxdAo05JoxI81ZAgMBAAECggEAdrdMRpPP2gY86wO9FlIflCkz8DZ9zeRFIYVwpqOkgL3iM8GzgmHQtSy+1n7eEbjQj4GkGmMn5bFKO37PNibRa2is9SJu6IiiTa93vy41B46NK3js6Yl+b6QQPJWyuPXIPaULbieDWI2Q2KuAi0HfJ8y2E+OuitCO8HTus3nQfjB9x0B2o4QQXKV9qhJF+Kj+vksBv8ErEyjrAGIGXMrqa6WiJEADPHK8V5s9+tfPJIkKTB7c6wpWaOCfKH7bH3P2Q2xrCqTjWkdwcEyvuJd4PUdGf6AS1BGMNhB46TWx4yRvbmF64gT+2OzbAH2M4FIwv4LMXLhanjNRZV2UaZ6azQKBgQDCDBN0dBcWaFlO8uggCUqGlzhfcvRroprx59nUPyFKPBvu0xdmrc+uacq7aBdebT2LtqlNg6kwIJZh3GiqFOtd11WWtGUhLGi2FTSXXCqSCZESDO3w1mjs4K8hvPsjt8heotblSHlx/ZQZkUCNNVl8S79Q7SPvyS5L6H7kfRIRVwKBgQC7zSHdhTB86EVkj+JQveICtw9Z9TGOzMupF1Vrm4jb+feYRUoSuqxL6AzQLFF17OYaIo7ResXSYkOXYcA4cTx7RVY5xP1hWF8tM5E4JYpMkIFXWLqy0o3+kDZj1VNeJ866BWGv9pkBV8F0sJwaSSFAgzYTHAgpD/72NQ6ykHF4zwKBgQCZQVBcORPC7scaQ15xbuUdR3GOcQv5toMJa0PvDa0NJEwrYyyoO7460fL78EoEmeJx5eHxBnspN5ON1rYTHlKF2vwN+oukl55bsIPIx1IX5NZemzeRbsWP0CSBUPBIVW460AFJNOQ100CUsNN6sxfpwylmkORBDXMzIHyXmPAcIQKBgQC48zZs0KSF/sGIXeNGHRqeDeaCc2UQ+0N/DUnEpfJMoYpjXamrdEgGoUstINhAyMN77Zi1WLHFlZx3U39Ylrw3sntOfWAEJGHnRCe6Ic5nxNB8Uxfa0F2i3CeUfhySV6XRsUcZYkQg/Gm7H8s8X9TtkcsJWsv2lJhFpM8KsBHwJQKBgCW3jK1YmiqjUxGzIQ2l61qZuZM5yaPz5bQQTuKAUMCWmlAoSIzFHCEHBt0xRRNlZg7PKntcaG+PGmEIljfiBldAW1x9a3ZCH8py4l6GbfAMsLnd6Fy8pf8BYlTaqls8P8l/zzqJtZL+UcmdIP14d4MWuQ31SukfbgcVxlIgqRuR";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAovTF0TYbPqa5gLhxJezyxLoXSm3sReExKZeUDTQZQtB8ZQewKEsn28N0yLZrxUQE60dQ1zX1WW0CrFR6LQ7fJONVBzQKGA+ulDg7AczYhnJG2my7eFoS2KZ9TRHgHV3/vCRf1fFtomiDRNV8vUqw46igoUSOsFWjR0KUlZ8oMjUPExQoaRz7Zk3SuqMZP2OKEHwvbx4C+S+DIy7svvYEb+c0tdrQWKI3sxTl8QgwXv1kFvNvfvwz5z0Q1d/boyo7+AElB5yhMJfk/gwwf3v86tTQKXra5VfoEdErlax42zw4RuuWSGdXRRFl7c6vGGRl9DhRlgQQx8YStvGUsrklKwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url ="http://35horwwme6.bjhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.anlimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type  = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
